package com.atguigu.boot.controller;

import com.atguigu.boot.bean.Person;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : leimingchao
 * @date Date : 2021年11月14日 13:01
 */
@RestController
public class ParameterTestController {

    /**
     * 数据绑定：页面提交的请求数据(GET,POST)都可以和对象属性进行绑定
     * @param person
     * @return
     */
    @PostMapping(value = "/saveUser")
    public Person saveUser(Person person) {
        return person;
    }

    @GetMapping(value = "/car/{id}/owner/{username}")
    public Map<String,Object> getCar(@PathVariable(value = "id") Integer id,
                                     @PathVariable(value = "username") String username,
                                     @PathVariable Map<String,String> pv,
                                     @RequestHeader(value = "User-Agent") String userAgent,
                                     @RequestHeader Map<String,String> header,
                                     @RequestParam(value = "age") Integer age,
                                     @RequestParam(value = "interests") List<String> interests,
                                     @RequestParam Map<String,String> params,
                                     @CookieValue(value = "_ga") String _ga,
                                     @CookieValue(value = "_ga") Cookie cookie) {
        Map<String,Object> map = new HashMap<>();
        // map.put("id",id);
        // map.put("name",username);
        // map.put("pv",pv);
        // map.put("userAgent",userAgent);
        // map.put("headers",header);
        map.put("age",age);
        map.put("interests",interests);
        map.put("params",params);
        map.put("_ga",_ga);
        System.out.println(cookie.getName() + "=====>" + cookie.getValue());
        return map;
    }

    @PostMapping(value = "/save")
    public Map<String,Object> postMethod(@RequestBody String content) {
        Map<String,Object> map = new HashMap<>();
        map.put("content",content);
        return map;
    }


    /**
     * 1.语法：/cars/sell;low=34;brand=byd,audi,yd
     * 2.SpringBoot默认是禁用了矩阵变量的功能
     * 手动开启：原理：对于路径的处理。UrlPathHelper进行解析。removeSemicolonContent(移除分号内容)支持矩阵变量
     * @param low
     * @param brand
     * @return
     */
    @GetMapping(value = "/cars/{path}")
    public Map<String,Object> carsSell(@MatrixVariable(value = "low") Integer low,
                                       @MatrixVariable(value = "brand") List<String> brand,
                                       @PathVariable(value = "path") String path) {
        Map<String,Object> map = new HashMap<>();
        map.put("low",low);
        map.put("brand",brand);
        map.put("path",path);
        return map;
    }

    // /boss/1;age=20/2;age=10
    @GetMapping(value = "/boss/{bossId}/{empId}")
    public Map<String,Object> boss(@MatrixVariable(value = "age",pathVar = "bossId") Integer bossAge,
                                   @MatrixVariable(value = "age",pathVar = "empId") Integer empAge) {
        Map<String,Object> map = new HashMap<>();
        map.put("bossAge",bossAge);
        map.put("empAge",empAge);
        return map;
    }
}
